| API Documentation |  |

- MAIN PAGE
- NAMESPACES
- CLASSES

Public Member Functions | Public Attributes | List of all members
nkMaths::Matrix Class Reference

Represents a 4x4 float matrix. More...

## Public Member Functions
| Matrix () noexcept
| Matrix (float m00, float m01, float m10, float m11) noexcept
| Matrix (float m00, float m01, float m02, float m10, float m11, float m12, float m20, float m21, float m22) noexcept
| Matrix (float m00, float m01, float m02, float m03, float m10, float m11, float m12, float m13, float m20, float m21, float m22, float m23, float m30, float m31, float m32, float m33) noexcept
| Matrix (const Matrix &other) noexcept
Vector | getRow0 () const
Vector | getRow1 () const
Vector | getRow2 () const
Vector | getRow3 () const
Vector | getCol0 () const
Vector | getCol1 () const
Vector | getCol2 () const
Vector | getCol3 () const
float | get (unsigned int row, unsigned int col) const
Matrix & | set (float val, unsigned int row, unsigned int col)
Matrix | getInverseMat2 () const
Matrix | getInverseMat3 () const
Matrix | getInverseMat4 () const
Matrix | getTranspose () const
Matrix & | inverseMat2 ()
Matrix & | inverseMat3 ()
Matrix & | inverseMat4 ()
Matrix & | transpose ()
Vector | getPositionComponent () const
Matrix | getOrientationComponent () const
Vector | getScaleComponent () const
void | getDecomposed (Vector &position, Matrix &orientation, Vector &scale) const
Matrix & | setToViewMatrixDirection (const Vector &position, const Vector &direction, const Vector &upDirection)
Matrix & | setToPerspectiveMatrix (float fov, float aspect, float near, float far)
Matrix & | setToPerspectiveReverseZMatrix (float fov, float aspect, float near, float far=-1.f)
Matrix & | setToOffCenterPerspectiveMatrix (float fovLeft, float fovRight, float fovTop, float fovBottom, float near, float far)
Matrix & | setToOffCenterPerspectiveReverseZMatrix (float fovLeft, float fovRight, float fovTop, float fovBottom, float near, float far=-1.f)
Matrix & | setToOrthographicMatrix (float widthNearPlane, float heightNearPlane, float near, float far)
Matrix & | setToOrthographicReverseZMatrix (float widthNearPlane, float heightNearPlane, float near, float far)
Matrix & | setPositionComponent (const Vector &position)
Matrix & | setOrientationComponent (const Quaternion &rot)
Matrix & | setScaleComponent (const Vector &scale)
Matrix & | setToTransformation (const Vector &position, const Quaternion &orientation, const Vector &scale)
float | getDeterminantMat2 () const
float | getDeterminantMat3 () const
float | getDeterminantMat4 () const
float | getTraceMat2 () const
float | getTraceMat3 () const
float | getTraceMat4 () const
nkMemory::String | toString () const
Matrix & | fromString (nkMemory::StringView str)
Matrix & | operator= (const Matrix &other) noexcept
bool | operator== (const Matrix &other)
bool | operator!= (const Matrix &other)
Matrix | operator+ (const Matrix &other) const
void | operator+= (const Matrix &other)
Matrix | operator- (const Matrix &other) const
void | operator-= (const Matrix &other)
Matrix | operator* (const Matrix &other) const
Vector | operator* (const Vector &vec) const
Quaternion | operator* (const Quaternion &q) const
Matrix | operator* (float scalar) const
void | operator*= (const Matrix &other)
void | operator*= (float scalar)
Matrix | operator/ (float scalar) const
void | operator/= (float scalar)

## Public Attributes
float | _m [4][4]
| The matrix memory, considered in row-major order (_m[row][col]).

## Detailed Description

Represents a 4x4 float matrix.

The memory layout is row major, meaning that _m[0] is the first line, and so on. The rule of thumb is that a matrix is designed to be used with the mathematical convention in mind (transforming a vector would be (M x V)).

## Constructor & Destructor Documentation

## ◆ Matrix() [1/5]

nkMaths::Matrix::Matrix | ( |  | ) |
| noexcept

Default constructor. Defaults to an identity matrix.

## ◆ Matrix() [2/5]

nkMaths::Matrix::Matrix | ( | float | m00,
|  | float | m01,
|  | float | m10,
|  | float | m11
| ) |  |
| noexcept

Matrix 2x2 constructor.

Parameters

  m00 | The value for _m[0][0].
  m01 | The value for _m[0][1].
  m10 | The value for _m[1][0].
  m11 | The value for _m[1][1].

Remarks
  Parameters are given row per row. Other members will default to identity.

## ◆ Matrix() [3/5]

nkMaths::Matrix::Matrix | ( | float | m00,
|  | float | m01,
|  | float | m02,
|  | float | m10,
|  | float | m11,
|  | float | m12,
|  | float | m20,
|  | float | m21,
|  | float | m22
| ) |  |
| noexcept

Matrix 3x3 constructor.

Parameters

  m00 | The value for _m[0][0].
  m01 | The value for _m[0][1].
  m02 | The value for _m[0][2].
  m10 | The value for _m[1][0].
  m11 | The value for _m[1][1].
  m12 | The value for _m[1][2].
  m20 | The value for _m[2][0].
  m21 | The value for _m[2][1].
  m22 | The value for _m[2][2].

Remarks
  Parameters are given row per row. Other members will default to identity.

## ◆ Matrix() [4/5]

nkMaths::Matrix::Matrix | ( | float | m00,
|  | float | m01,
|  | float | m02,
|  | float | m03,
|  | float | m10,
|  | float | m11,
|  | float | m12,
|  | float | m13,
|  | float | m20,
|  | float | m21,
|  | float | m22,
|  | float | m23,
|  | float | m30,
|  | float | m31,
|  | float | m32,
|  | float | m33
| ) |  |
| noexcept

Matrix 4x4 constructor.

Parameters

  m00 | The value for _m[0][0].
  m01 | The value for _m[0][1].
  m02 | The value for _m[0][2].
  m03 | The value for _m[0][3].
  m10 | The value for _m[1][0].
  m11 | The value for _m[1][1].
  m12 | The value for _m[1][2].
  m13 | The value for _m[1][3].
  m20 | The value for _m[2][0].
  m21 | The value for _m[2][1].
  m22 | The value for _m[2][2].
  m23 | The value for _m[2][3].
  m30 | The value for _m[3][0].
  m31 | The value for _m[3][1].
  m32 | The value for _m[3][2].
  m33 | The value for _m[3][3].

Remarks
  Parameters are given row per row.

## ◆ Matrix() [5/5]

nkMaths::Matrix::Matrix | ( | const Matrix & | other | ) |
| noexcept

Copy constructor.

Parameters

  other | The matrix to copy from.

## Member Function Documentation

## ◆ getRow0()

Vector nkMaths::Matrix::getRow0 | ( |  | ) | const

Returns
  The first row of the matrix, as a vector.

## ◆ getRow1()

Vector nkMaths::Matrix::getRow1 | ( |  | ) | const

Returns
  The second row of the matrix, as a vector.

## ◆ getRow2()

Vector nkMaths::Matrix::getRow2 | ( |  | ) | const

Returns
  The third row of the matrix, as a vector.

## ◆ getRow3()

Vector nkMaths::Matrix::getRow3 | ( |  | ) | const

Returns
  The fourth row of the matrix, as a vector.

## ◆ getCol0()

Vector nkMaths::Matrix::getCol0 | ( |  | ) | const

Returns
  The first column of the matrix, as a vector.

## ◆ getCol1()

Vector nkMaths::Matrix::getCol1 | ( |  | ) | const

Returns
  The second column of the matrix, as a vector.

## ◆ getCol2()

Vector nkMaths::Matrix::getCol2 | ( |  | ) | const

Returns
  The third column of the matrix, as a vector.

## ◆ getCol3()

Vector nkMaths::Matrix::getCol3 | ( |  | ) | const

Returns
  The fourth column of the matrix, as a vector.

## ◆ get()

float nkMaths::Matrix::get | ( | unsigned int | row,
|  | unsigned int | col
| ) |  | const

Parameters

  row | The row coordinate of the member to retrieve, between [0, 3].
  col | The col coordinate of the member to retrieve, between [0, 3].

Returns
  The component requested (_m[row][col]) if available, 0 in all other cases.

Remarks
  This function does a bound check. If you are in a safe environment and can index the array directly, it is advised to do so.

## ◆ set()

Matrix& nkMaths::Matrix::set | ( | float | val,
|  | unsigned int | row,
|  | unsigned int | col
| ) |  |

Parameters

  val | The value to set in the matrix.
  row | The row coordinate to override, between [0, 3].
  col | The col coordinate to override, between [0, 3].

Returns
  Reference over caller.

Remarks
  This function does a bound check. If you are in a safe environment and can index the array directly, it is advised to do so.

## ◆ getInverseMat2()

Matrix nkMaths::Matrix::getInverseMat2 | ( |  | ) | const

Returns
  The matrix's inverse, as if the matrix was a 2x2 matrix.

## ◆ getInverseMat3()

Matrix nkMaths::Matrix::getInverseMat3 | ( |  | ) | const

Returns
  The matrix's inverse, as if the matrix was a 3x3 matrix.

## ◆ getInverseMat4()

Matrix nkMaths::Matrix::getInverseMat4 | ( |  | ) | const

Returns
  The matrix's inverse, as if the matrix was a 4x4 matrix.

## ◆ getTranspose()

Matrix nkMaths::Matrix::getTranspose | ( |  | ) | const

Returns
  The transposed matrix.

## ◆ inverseMat2()

Matrix& nkMaths::Matrix::inverseMat2 | ( |  | ) |

Inverses the matrix, as if it was a 2x2 matrix.

Returns
  The matrix the function has been called on, for operation chaining.

## ◆ inverseMat3()

Matrix& nkMaths::Matrix::inverseMat3 | ( |  | ) |

Inverses the matrix, as if it was a 3x3 matrix.

Returns
  The matrix the function has been called on, for operation chaining.

## ◆ inverseMat4()

Matrix& nkMaths::Matrix::inverseMat4 | ( |  | ) |

Inverses the matrix, as if it was a 4x4 matrix.

Returns
  The matrix the function has been called on, for operation chaining.

## ◆ transpose()

Matrix& nkMaths::Matrix::transpose | ( |  | ) |

Transposes the matrix.

Returns
  The matrix the function has been called on, for operation chaining.

## ◆ getPositionComponent()

Vector nkMaths::Matrix::getPositionComponent | ( |  | ) | const

Returns
  The position component, considering the matrix as a 4x4 transformation matrix.

Remarks
  The matrix being row major, this corresponds to the third column's first 3 members.

## ◆ getOrientationComponent()

Matrix nkMaths::Matrix::getOrientationComponent | ( |  | ) | const

Returns
  The orientation component, considering the matrix as a 4x4 transformation matrix.

Remarks
  The matrix being row major, this corresponds to the internals 3x3 matrix, once the scale has been stripped out.

## ◆ getScaleComponent()

Vector nkMaths::Matrix::getScaleComponent | ( |  | ) | const

Returns
  The scale component, considering the matrix as a 4x4 transformation matrix.

Remarks
  The matrix being row major, this corresponds to the lengths of each of the 3 first columns.

## ◆ getDecomposed()

void nkMaths::Matrix::getDecomposed | ( | Vector & | position,
|  | Matrix & | orientation,
|  | Vector & | scale
| ) |  | const

Decomposes the matrix into a set of translation, orientation, and scale, considering the matrix as a 4x4 transformation matrix.

Parameters

  position | The position, translation component to fill.
  orientation | The orientation component to fill.
  scale | The scale component to fill.

## ◆ setToViewMatrixDirection()

Matrix& nkMaths::Matrix::setToViewMatrixDirection | ( | const Vector & | position,
|  | const Vector & | direction,
|  | const Vector & | upDirection
| ) |  |

Sets the matrix as a view matrix.

Parameters

  position | The eye position.
  direction | The eye front direction.
  upDirection | The eye up direction.

Returns
  Reference over caller.

## ◆ setToPerspectiveMatrix()

Matrix& nkMaths::Matrix::setToPerspectiveMatrix | ( | float | fov,
|  | float | aspect,
|  | float | near,
|  | float | far
| ) |  |

Sets the matrix as a perspective matrix.

Parameters

  fov | The field of view, vertical, in radians.
  aspect | The aspect ratio, given by width over height.
  near | The near plane distance in world units.
  far | The far plane distance in world units.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [0, 1], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [-1, 1].

## ◆ setToPerspectiveReverseZMatrix()

Matrix& nkMaths::Matrix::setToPerspectiveReverseZMatrix | ( | float | fov,
|  | float | aspect,
|  | float | near,
|  | float | far = `-1.f`
| ) |  |

Sets the matrix as a perspective matrix, with a reversed Z (mapping near to 1 and far to 0). This is an interesting characteristic to be able to work with reversed Z buffers, improving floating point precision distribution within the frustum. Doing so helps fighting Z-fighting, particularly at far distances with big [near, far] interval values.

Parameters

  fov | The field of view, vertical, in radians.
  aspect | The aspect ratio, given by width over height.
  near | The near plane distance in world units.
  far | The far plane distance in world units. Leave it to -1 to create an infinite depth projection matrix.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [1, 0], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [1, -1].

## ◆ setToOffCenterPerspectiveMatrix()

Matrix& nkMaths::Matrix::setToOffCenterPerspectiveMatrix | ( | float | fovLeft,
|  | float | fovRight,
|  | float | fovTop,
|  | float | fovBottom,
|  | float | near,
|  | float | far
| ) |  |

Sets the matrix as an off-center perspective matrix. This particularity can move the vanishing point away from the usual center of the created frustum. It is for instance necessary when creating projection matrices for each eye in virtual reality configurations.

Parameters

  fovLeft | The left field of view angle, horizontal from the normal frustum center, in radians.
  fovRight | The right field of view angle, horizontal from the normal frustum center, in radians.
  fovTop | The top field of view angle, vertical from the normal frustum center, in radians.
  fovBottom | The bottom field of view angle, vertical from the normal frustum center, in radians.
  near | The near plane distance in world units.
  far | The far plane distance in world units.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [0, 1], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [-1, 1].

## ◆ setToOffCenterPerspectiveReverseZMatrix()

Matrix& nkMaths::Matrix::setToOffCenterPerspectiveReverseZMatrix | ( | float | fovLeft,
|  | float | fovRight,
|  | float | fovTop,
|  | float | fovBottom,
|  | float | near,
|  | float | far = `-1.f`
| ) |  |

Sets the matrix as an off-center perspective matrix, with a reversed Z (mapping near to 1 and far to 0). This particularity can move the vanishing point away from the usual center of the created frustum. It is for instance necessary when creating projection matrices for each eye in virtual reality configurations. See setToPerspectiveReverseZMatrix() for more details about the reversed Z particularity.

Parameters

  fovLeft | The left field of view angle, horizontal from the normal frustum center, in radians.
  fovRight | The right field of view angle, horizontal from the normal frustum center, in radians.
  fovTop | The top field of view angle, vertical from the normal frustum center, in radians.
  fovBottom | The bottom field of view angle, vertical from the normal frustum center, in radians.
  near | The near plane distance in world units.
  far | The far plane distance in world units. Leave it to -1 to create an infinite depth projection matrix.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [1, 0], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [1, -1].

## ◆ setToOrthographicMatrix()

Matrix& nkMaths::Matrix::setToOrthographicMatrix | ( | float | widthNearPlane,
|  | float | heightNearPlane,
|  | float | near,
|  | float | far
| ) |  |

Sets the matrix as an orthographic matrix.

Parameters

  widthNearPlane | The width of the matrix, in eye space distance.
  heightNearPlane | The height of the matrix, in eye space distance.
  near | The near plane distance in world units.
  far | The far plane distance in world units.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [0, 1], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [-1, 1].

## ◆ setToOrthographicReverseZMatrix()

Matrix& nkMaths::Matrix::setToOrthographicReverseZMatrix | ( | float | widthNearPlane,
|  | float | heightNearPlane,
|  | float | near,
|  | float | far
| ) |  |

Sets the matrix as an orthographic matrix, with a reversed Z (mapping near to 1 and far to 0). See setToPerspectiveReverseZMatrix() for more details about the reversed Z particularity.

Parameters

  widthNearPlane | The width of the matrix, in eye space distance.
  heightNearPlane | The height of the matrix, in eye space distance.
  near | The near plane distance in world units.
  far | The far plane distance in world units.

Returns
  Reference over caller.

Remarks
  The function will map the z component to [1, 0], in DirectX's style. Beware that OpenGL usually expects the projection's z to end between [1, -1].

## ◆ setPositionComponent()

Matrix& nkMaths::Matrix::setPositionComponent | ( | const Vector & | position | ) |

Sets the position component in the matrix.

Parameters

  position | The position to set in the matrix.

Returns
  Reference over caller.

## ◆ setOrientationComponent()

Matrix& nkMaths::Matrix::setOrientationComponent | ( | const Quaternion & | rot | ) |

Sets the rotation component in the matrix.

Parameters

  rot | The quaternion to use as orientation.

Returns
  Reference over caller.

## ◆ setScaleComponent()

Matrix& nkMaths::Matrix::setScaleComponent | ( | const Vector & | scale | ) |

Sets the scale component in the matrix.

Parameters

  scale | The scale on each axis.

Returns
  Reference over caller.

## ◆ setToTransformation()

Matrix& nkMaths::Matrix::setToTransformation | ( | const Vector & | position,
|  | const Quaternion & | orientation,
|  | const Vector & | scale
| ) |  |

Sets the matrix as a full transformation matrix.

Parameters

  position | The position, translation component to bake.
  orientation | The orientation component to bake.
  scale | The scale component to bake.

Returns
  Reference over caller.

## ◆ getDeterminantMat2()

float nkMaths::Matrix::getDeterminantMat2 | ( |  | ) | const

Returns
  The determinant of the matrix, considering it as a 2x2 matrix.

## ◆ getDeterminantMat3()

float nkMaths::Matrix::getDeterminantMat3 | ( |  | ) | const

Returns
  The determinant of the matrix, considering it as a 3x3 matrix.

## ◆ getDeterminantMat4()

float nkMaths::Matrix::getDeterminantMat4 | ( |  | ) | const

Returns
  The determinant of the matrix, considering it as a 4x4 matrix.

## ◆ getTraceMat2()

float nkMaths::Matrix::getTraceMat2 | ( |  | ) | const

Returns
  The trace of the matrix, considering it as a 2x2 matrix.

## ◆ getTraceMat3()

float nkMaths::Matrix::getTraceMat3 | ( |  | ) | const

Returns
  The trace of the matrix, considering it as a 3x3 matrix.

## ◆ getTraceMat4()

float nkMaths::Matrix::getTraceMat4 | ( |  | ) | const

Returns
  The trace of the matrix, considering it as a 4x4 matrix.

## ◆ toString()

nkMemory::String nkMaths::Matrix::toString | ( |  | ) | const

Returns
  The matrix expressed as a string, formatted as "m00, m01, m02, m03, m10, ...".

## ◆ fromString()

Matrix& nkMaths::Matrix::fromString | ( | nkMemory::StringView | str | ) |

Interprets a string to set the components of the matrix. Format is m00,m01,m02,m03,...

Parameters

  str | The string to parse.

Returns
  The updated matrix instance.

## ◆ operator=()

Matrix& nkMaths::Matrix::operator= | ( | const Matrix & | other | ) |
| noexcept

Assignment operator.

Parameters

  other | The matrix to assign.

Returns
  The calling matrix, once assigned.

## ◆ operator==()

bool nkMaths::Matrix::operator== | ( | const Matrix & | other | ) |

Equality operator.

Parameters

  other | The matrix to check against.

Returns
  If the matrices are equal (true) or not (false).

## ◆ operator!=()

bool nkMaths::Matrix::operator!= | ( | const Matrix & | other | ) |

Inequality operator.

Parameters

  other | The matrix to check against.

Returns
  If the matrices are different (true) or not (false).

## ◆ operator+()

Matrix nkMaths::Matrix::operator+ | ( | const Matrix & | other | ) | const

Addition operator.

Parameters

  other | The matrix to operate with.

Returns
  A matrix resulting from the operation.

## ◆ operator+=()

void nkMaths::Matrix::operator+= | ( | const Matrix & | other | ) |

Addition and assignment operator.

Parameters

  other | The matrix to operate with.

## ◆ operator-()

Matrix nkMaths::Matrix::operator- | ( | const Matrix & | other | ) | const

Subtraction operator.

Parameters

  other | The matrix to operate with.

Returns
  A matrix resulting from the operation.

## ◆ operator-=()

void nkMaths::Matrix::operator-= | ( | const Matrix & | other | ) |

Subtraction and assignment operator.

Parameters

  other | The matrix to operate with.

## ◆ operator*() [1/4]

Matrix nkMaths::Matrix::operator* | ( | const Matrix & | other | ) | const

Transformation operator.

Parameters

  other | The matrix to operate with.

Returns
  A matrix resulting from the operation.

## ◆ operator*() [2/4]

Vector nkMaths::Matrix::operator* | ( | const Vector & | vec | ) | const

Transformation operator.

Parameters

  vec | The vector to apply to.

Returns
  A vector resulting from the operation.

## ◆ operator*() [3/4]

Quaternion nkMaths::Matrix::operator* | ( | const Quaternion & | q | ) | const

Transformation operator.

Parameters

  q | The quaternion to apply to.

Returns
  A quaternion resulting from the operation.

Remarks
  This operation will only use the rotation component from the matrix to compose both rotations.

## ◆ operator*() [4/4]

Matrix nkMaths::Matrix::operator* | ( | float | scalar | ) | const

Multiplication operator.

Parameters

  scalar | The scalar to operate with.

Returns
  A matrix resulting from the operation.

## ◆ operator*=() [1/2]

void nkMaths::Matrix::operator*= | ( | const Matrix & | other | ) |

Transformation and assignment operator.

Parameters

  other | The matrix to operate with.

## ◆ operator*=() [2/2]

void nkMaths::Matrix::operator*= | ( | float | scalar | ) |

Multiplication and assignment operator.

Parameters

  scalar | The scalar to operate with.

## ◆ operator/()

Matrix nkMaths::Matrix::operator/ | ( | float | scalar | ) | const

Division operator.

Parameters

  scalar | The scalar to operate with.

Returns
  A matrix resulting from the operation.

## ◆ operator/=()

void nkMaths::Matrix::operator/= | ( | float | scalar | ) |

Addition and assignment operator.

Parameters

  scalar | The scalar to operate with.

The documentation for this class was generated from the following file:

- Documentation/Headers/NilkinsMaths/Algebra/Matrix.h